package games.isoMatric.ui
{
	import core.Global;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import games.isoMatric.data.Player;
	
	public class Role extends MovieClip
	{
		private var m_data:Player;
		
		private var m_head:MovieClip;
		private var m_body:MovieClip;
		private var m_face:MovieClip;
		
		public function Role(data:Player)
		{
			super();
			m_data = data;
			mouseEnabled = false;
			mouseChildren = false;
			init();
		}
		
		private function init():void
		{
			m_data.addEventListener(Player.STATE_CHANGE,onStateChange);
			m_data.addEventListener(Player.DIRECTION_CHANGE,onDirectionChange);
			
			m_face = Global.loadResource(GFrame.MAIN_UI,"PubSHeadMale" ).getChildAt(0);
			m_head = Global.loadResource(GFrame.MAIN_UI,"SceneHead").getChildAt(0);
			m_body = Global.loadResource(GFrame.MAIN_UI,"SceneDress").getChildAt(0);
			
			addChild(m_face);
			addChild(m_body);
			addChild(m_head);
			
			m_face.x = m_body.x = m_head.x = -35;
			m_face.y = m_body.y = m_head.y = -75;
			
			onDirectionChange();
			onStateChange();
		}
		
		protected function onDirectionChange(event:Event = null):void
		{
			// TODO Auto-generated method stub
			var name:String;
			switch(m_data.direction)
			{
				case 0:
					name = "se";
					break;
				case 1:
					name = "e";
					break;
				case 2:
					name = "ne";
					break;
				case 3:
					name = "n";
					break;
				case 4:
					name = "nw";
					break;
				case 5:
					name = "w";
					break;
				case 6:
					name = "sw";
					break;
				case 7:
					name = "s";
					break;
				
			}
			m_face.gotoAndStop(name);
			m_head.gotoAndStop(name);
			m_body.gotoAndStop(name);
		}
		
		protected function onStateChange(event:Event = null):void
		{
			// TODO Auto-generated method stub
			switch(m_data.state)
			{
				case Player.M_STAND:
					m_face.stop();
					m_head.stop();
					m_body.stop();
					if(m_body.numChildren > 0)
					{
						MovieClip(m_body.getChildAt(0)).gotoAndStop(1);
					}
					break;
				case Player.M_WALK:
					m_face.gotoAndStop(m_data.direction);
					m_head.gotoAndStop(m_data.direction);
					m_body.gotoAndStop(m_data.direction);
					if(m_body.numChildren > 0)
					{
						MovieClip(m_body.getChildAt(0)).play();
					}
					break;
			}
		}
	}
}